package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Title: SetmealController
 * @Package com.itheima.reggie.controller
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/15 9:28
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;

    //新增套餐
    @PostMapping
    @CacheEvict(value = "categoryCache",allEntries = true)
    public R getSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.AddSetmealAndSetmealDishes(setmealDto);
        return R.success("保存套餐成功");
    }


    //根据名称条件进行分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    //根据id，来删除套餐
    @DeleteMapping
    @CacheEvict(value = "categoryCache",allEntries = true)
    public R<String> delect(@RequestParam("ids") List<String> ids){
        setmealService.delectSetmeal(ids);
        return R.success("套餐删除成功");

    }

    //停售该套餐
    @PostMapping("/status/0")
    public R<String> updatestatus(@RequestParam("ids") List<String> ids){

        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.set(Setmeal::getStatus,0);
        setmealService.update(lqw);
         return R.success("停售成功");
    }

    //启售该套餐
    @PostMapping("/status/1")
    public R<String> updatestatus2(@RequestParam("ids") List<String> ids){
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.set(Setmeal::getStatus,1);
        setmealService.update(lqw);
        return R.success("启售成功");
    }

    //根据id查询套餐
    @Cacheable(value = "categoryCache" ,key = "#p0")
    @GetMapping("/{id}")
    public R<SetmealDto> looksetmealdishes(@PathVariable Long id){
        SetmealDto res =setmealService. looksetmealdishes(id);
        return R.success(res);
    }

     //修改菜品
     @PutMapping
    private R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
         setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
     }

     //查询该套餐分类下的所有套餐
     @Cacheable(value = "categoryCache" ,key = "#p0+'_'+#p1")
     @GetMapping("/list")
    public R<List<Setmeal>> getSetmeal(Long categoryId,String status){
         LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
         lqw.eq(Setmeal::getCategoryId,categoryId);
         lqw.eq(Setmeal::getStatus,status);
         List<Setmeal> list = setmealService.list(lqw);
         return R.success(list);
     }

}
